#include "page5.h"

page5::page5(QWidget *parent)
	: QWidget(parent)
{
	this->setWindowIcon(QIcon("Image\\运动会.svg"));
	QPixmap pixmap("Image\\光标.svg");
	pixmap = pixmap.scaled(30, 24, Qt::KeepAspectRatio);
	QCursor cursor(pixmap);
	this->setCursor(cursor);

	this->customPlot = new QCustomPlot(this);
	this->customPlot->setGeometry(75, 100, 500, 450);

	this->parent = dynamic_cast<SportMeeting*>(parent);
	this->teamlabel = new QStringList();

	ui.setupUi(this);
	connect(ui.comboBox, &QComboBox::currentIndexChanged, this, &page5::myPaint);
	connect(ui.comboBox_2, &QComboBox::currentIndexChanged, this, &page5::myPaint);
	connect(ui.comboBox_3, &QComboBox::currentIndexChanged, this, &page5::myPaint);
	connect(ui.pushButton, &QPushButton::clicked, this, &page5::pre_page);
	connect(ui.pushButton_2, &QPushButton::clicked, this, &page5::savePdf);

}

page5::~page5()
{
	
}
void page5:: setData(MyData* data) {
	this->myData = data;
}
void page5::painterSum() {
	customPlot->clearPlottables();  // 移除所有图形（柱状图、曲线等）
	customPlot->clearItems();       // 清除所有文本、线段等自定义项

	int teamNum = this->myData->getTeamNum();
	int itemNum1 = this->myData->getItemNum1();
	int itemNum2 = this->myData->getItemNum2();
	QVector<team>& teams = this->myData->getTeams();
	QVector<QString> classesx(teamNum,"");
	QVector<double> sumy(teamNum, 0);
	QVector<double> xPos;
	for (int i = 0; i < teamNum; i++) {
		sumy[i] = teams[i].sumVal(itemNum1+itemNum2);
		classesx[i] = teams[i].getName();
		xPos << (i + 1);
	}
	
	QCPBars* bars = new QCPBars(this->customPlot->xAxis, this->customPlot->yAxis);
	bars->setData(xPos, sumy);
	bars->setName("得分总和");
	bars->setPen(QPen(Qt::darkCyan)); // 边框颜色
	bars->setBrush(QBrush(Qt::cyan)); // 填充颜色

	// 创建文本刻度的坐标轴设置器
	QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
	for (int i = 0; i < xPos.size(); ++i) {
		textTicker->addTick(xPos[i], classesx[i]);  // 将每个柱子位置关联标签
	}
	customPlot->xAxis->setTicker(textTicker);
	//customPlot->xAxis->setLabel("参赛队伍");     // X轴标签
	customPlot->yAxis->setLabel("得分");
	customPlot->xAxis->setRange(0, teamNum+1);
	auto max = std::max_element(sumy.begin(), sumy.end());
	customPlot->yAxis->setRange(0, *max+2);

	// 设置坐标轴网格线
	customPlot->xAxis->grid()->setVisible(false);   // X轴不显示网格线
	customPlot->yAxis->grid()->setSubGridVisible(true);

	// 设置背景
	customPlot->setBackground(QBrush(QColor(245, 245, 245)));

	// 显示图例并设置位置
	customPlot->legend->setVisible(true);
	//customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignCenter | Qt::AlignTop);

	for (int i = 0; i < xPos.size(); ++i) {
		QCPItemText* textLabel = new QCPItemText(customPlot);
		textLabel->position->setCoords(xPos[i], sumy[i] + 0.5);  // 在柱子上方显示
		textLabel->setText(QString::number(sumy[i]));
		textLabel->setFont(QFont("Arial", 10));
		textLabel->setPositionAlignment(Qt::AlignCenter | Qt::AlignBottom);
		textLabel->setBrush(Qt::white);
	}
	customPlot->replot();
}
void page5::painterFirst() {
	customPlot->clearPlottables();  // 移除所有图形（柱状图、曲线等）
	customPlot->clearItems();       // 清除所有文本、线段等自定义项
	int teamNum = this->myData->getTeamNum();
	int itemNum1 = this->myData->getItemNum1();
	int itemNum2 = this->myData->getItemNum2();
	QVector<team>& teams = this->myData->getTeams();

	QVector<double> x;
	QVector<QString> labels(teamNum, "");
	QVector<double> y1 = this->myData->getFirstRankCount1();
	QVector<double> y2 = this->myData->getFirstRankCount2();
	for (int i = 0; i < teamNum; i++) {
		labels[i] = teams[i].getName();
		x << (i + 1);
		y1[i] = y1[i] / (itemNum1 + itemNum2);
		y2[i] = y2[i] / (itemNum1 + itemNum2);
	}


	QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
	for (int i = 0; i < x.size(); ++i) {
		textTicker->addTick(x[i], labels[i]);
	}
	customPlot->xAxis->setTicker(textTicker);

	QCPBars* bars1 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
	QCPBars* bars2 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
	bars2->setStackingGap(0); // 堆叠间距（0表示紧密堆叠）
	bars2->moveAbove(bars1);

	bars1->setData(x, y1);
	bars1->setName("男生项目");
	bars1->setPen(QPen(Qt::darkRed));
	bars1->setBrush(QColor(220, 80, 80));

	bars2->setData(x, y2);
	bars2->setName("女生项目");
	bars2->setPen(QPen(Qt::darkGreen));
	bars2->setBrush(QColor(80, 180, 80));

	double barWidth = 0.7; // 相对宽度（0-1之间）
	bars1->setWidth(barWidth);
	bars2->setWidth(barWidth);

	customPlot->yAxis->setLabel("夺冠率");

	QVector<double> y(teamNum, 0);
	for (int i = 0; i < teamNum; i++) {
		y[i] = y1[i] + y2[i];
	}
	auto max = std::max_element(y.begin(), y.end());
	customPlot->yAxis->setRange(0, *max+ 0.1);
	customPlot->xAxis->setRange(0, x.last() + 1);

	for (int i = 0; i < x.size(); ++i) {
		double total = y1[i] + y2[i];
		QCPItemText* totalLabel = new QCPItemText(customPlot);
		totalLabel->position->setAxes(customPlot->xAxis, customPlot->yAxis);
		totalLabel->position->setCoords(x[i], total);
		totalLabel->setPositionAlignment(Qt::AlignCenter | Qt::AlignBottom);
		totalLabel->setText(QString::number(total * 100, 'f', 1) + "%");
		totalLabel->setFont(QFont("Arial", 9, QFont::Bold));
		totalLabel->setPen(QPen(Qt::black));
	}

	customPlot->replot();
}
void page5::painterThird() {
	customPlot->clearPlottables();  // 移除所有图形（柱状图、曲线等）
	customPlot->clearItems();       // 清除所有文本、线段等自定义项
	int teamNum = this->myData->getTeamNum();
	int itemNum1 = this->myData->getItemNum1();
	int itemNum2 = this->myData->getItemNum2();
	QVector<team>& teams = this->myData->getTeams();

	QVector<double> x;
	QVector<QString> labels(teamNum, "");
	QVector<double> y1 = this->myData->getThirdRankCount1();
	QVector<double> y2 = this->myData->getThirdRankCount2();
	for (int i = 0; i < teamNum; i++) {
		labels[i] = teams[i].getName();
		x << (i + 1);
		y1[i] = y1[i] / (itemNum1 + itemNum2);
		y2[i] = y2[i] / (itemNum1 + itemNum2);
	}


	QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
	for (int i = 0; i < x.size(); ++i) {
		textTicker->addTick(x[i], labels[i]);
	}
	customPlot->xAxis->setTicker(textTicker);

	QCPBars* bars1 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
	QCPBars* bars2 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
	bars2->setStackingGap(0); // 堆叠间距（0表示紧密堆叠）
	bars2->moveAbove(bars1);

	bars1->setData(x, y1);
	bars1->setName("男生项目");
	bars1->setPen(QPen(Qt::darkRed));
	bars1->setBrush(QColor(220, 80, 80));

	bars2->setData(x, y2);
	bars2->setName("女生项目");
	bars2->setPen(QPen(Qt::darkGreen));
	bars2->setBrush(QColor(80, 180, 80));

	double barWidth = 0.7; // 相对宽度（0-1之间）
	bars1->setWidth(barWidth);
	bars2->setWidth(barWidth);

	customPlot->yAxis->setLabel("前三率");

	QVector<double> y(teamNum, 0);
	for (int i = 0; i < teamNum; i++) {
		y[i] = y1[i] + y2[i];
	}
	auto max = std::max_element(y.begin(), y.end());
	customPlot->yAxis->setRange(0, *max + 0.1);
	customPlot->xAxis->setRange(0, x.last() + 1);

	for (int i = 0; i < x.size(); ++i) {
		double total = y1[i] + y2[i];
		QCPItemText* totalLabel = new QCPItemText(customPlot);
		totalLabel->position->setAxes(customPlot->xAxis, customPlot->yAxis);
		totalLabel->position->setCoords(x[i], total);
		totalLabel->setPositionAlignment(Qt::AlignCenter | Qt::AlignBottom);
		totalLabel->setText(QString::number(total*100, 'f', 1) + "%");
		totalLabel->setFont(QFont("Arial", 9, QFont::Bold));
		totalLabel->setPen(QPen(Qt::black));
	}
	customPlot->replot();
}
void page5::painterRadar() {
	int index1 = ui.comboBox_2->currentIndex();
	int index2 = ui.comboBox_3->currentIndex();
	QString name1 = ui.comboBox_2->currentText();
	QString name2 = ui.comboBox_3->currentText();
	customPlot->clearPlottables();  // 移除所有图形（柱状图、曲线等）
	customPlot->clearItems();       // 清除所有文本、线段等自定义项
	// 1. 设置雷达图参数

	const int dimensions = this->myData->getItemNum1()+this->myData->getItemNum2();      // 维度数量
	const double maxRadius = this->myData->getTeamNum();								// 最大半径
	QVector<boyItem> boyItems = this->myData->getBoyItems();
	QVector<girlItem> girlItems = this->myData->getGirlItems();
	QStringList labels;
	QList<double> value1(dimensions,0), value2(dimensions, 0);
	for (int i = 0; i < dimensions; i++) {
		if (i < boyItems.size()) {
			labels.append(boyItems[i].getName());
			value1[i] = maxRadius - boyItems[i].getRank()[index1]+1;
			value2[i] = maxRadius - boyItems[i].getRank()[index2]+1;
		}
		else {
			labels.append(girlItems[i - boyItems.size()].getName());
			value1[i] = maxRadius - girlItems[i - boyItems.size()].getRank()[index1] + 1;
			value2[i] = maxRadius - girlItems[i - boyItems.size()].getRank()[index2] + 1;
		}
	}

	// 2. 准备坐标系
	customPlot->xAxis->setRange(-1* maxRadius-1, maxRadius+1);
	customPlot->yAxis->setRange(-1* maxRadius-1, maxRadius+1);
	customPlot->xAxis->setVisible(false);
	customPlot->yAxis->setVisible(false);

	// 3. 绘制坐标轴和网格
	for (int i = 0; i < dimensions; ++i) {
		double angle = 2 * M_PI * i / dimensions;
		double x = maxRadius * cos(angle);
		double y = maxRadius * sin(angle);

		// 坐标轴线
		QCPItemLine* axis = new QCPItemLine(customPlot);
		axis->start->setCoords(0, 0);
		axis->end->setCoords(x, y);
		axis->setPen(QPen(Qt::gray));

		// 添加标签
		QCPItemText* label = new QCPItemText(customPlot);
		label->position->setCoords(1.1 * x, 1.1 * y); // 向外偏移
		label->setText(labels[i]);
		label->setFont(QFont("Microsoft YaHei", 9));
	}

	// 4. 绘制网格圆环
	const int gridCircles = maxRadius;
	for (int g = 1; g <= gridCircles; ++g) {
		double radius = maxRadius * g / gridCircles;
		QCPCurve* gridCircle = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
		QVector<QCPCurveData> gridData;

		for (int i = 0; i <= 100; ++i) {
			double angle = 2 * M_PI * i / 100;
			gridData.append(QCPCurveData(i, radius * cos(angle), radius * sin(angle)));
		}
		gridCircle->data()->set(gridData);
		gridCircle->setPen(QPen(QColor(200, 200, 200), 1, Qt::DotLine));
	}
	// 绘制数据区域
	QCPCurve* dataCurve1 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
	QVector<QCPCurveData> curveData1;

	for (int i = 0; i < dimensions; ++i) {
		double angle = 2 * M_PI * i / dimensions;
		double value = value1[i];
		curveData1.append(QCPCurveData(i, value * cos(angle), value * sin(angle)));
	}
	// 闭合曲线（连接首尾点）
	curveData1.append(QCPCurveData(dimensions, curveData1.at(0).key, curveData1.at(0).value));

	// 设置数据和样式
	dataCurve1->data()->set(curveData1);
	dataCurve1->setPen(QPen(QColor(65, 105, 225), 2));
	dataCurve1->setBrush(QBrush(QColor(65, 105, 225, 50))); // 半透明填充
	dataCurve1->setName(name1);				// 设置图例名称
	// 绘制数据区域
	QCPCurve* dataCurve2 = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
	QVector<QCPCurveData> curveData2;

	for (int i = 0; i < dimensions; ++i) {
		double angle = 2 * M_PI * i / dimensions;
		double value = value2[i];
		curveData2.append(QCPCurveData(i, value * cos(angle), value * sin(angle)));
	}
	// 闭合曲线（连接首尾点）
	curveData2.append(QCPCurveData(dimensions, curveData2.at(0).key, curveData2.at(0).value));

	// 设置数据和样式
	dataCurve2->data()->set(curveData2);
	dataCurve2->setPen(QPen(QColor(255, 140, 0), 2));
	dataCurve2->setBrush(QBrush(QColor(255, 140, 0, 50))); // 半透明填充
	dataCurve2->setName(name2);				// 设置图例名称
	// 图例
	int n = customPlot->legend->itemCount();
	for (int i = 0; i <= n-3; ++i) {
		customPlot->legend->removeItem(0);
	}
	customPlot->legend->setBrush(QBrush(QColor(255, 255, 255, 200))); // 背景半透明白色
	customPlot->legend->setBorderPen(QPen(Qt::lightGray)); // 边框颜色
	customPlot->legend->setFont(QFont("Microsoft YaHei", 9)); // 设置字体
	customPlot->legend->setVisible(true); // 显示图例

	customPlot->replot();
}
void page5::myPaint() {
	ui.comboBox_2->setEnabled(false);
	ui.comboBox_3->setEnabled(false);
	int x = this->myData->getTeamNum();
	int y = this->myData->getItemNum1() + this->myData->getItemNum2();
	customPlot->xAxis->setRange(0, x);
	customPlot->yAxis->setRange(0, y);
	customPlot->yAxis->setLabel("");
	customPlot->xAxis->setVisible(true);
	customPlot->yAxis->setVisible(true);
	customPlot->legend->setVisible(false);
	int index = ui.comboBox->currentIndex();
	switch (index) {
	case 0:
		customPlot->clearPlottables();
		customPlot->clearItems();   
		customPlot->replot();
		break;
	case 1:
		customPlot->legend->setVisible(true);
		painterSum();
		break;
	case 2:
		customPlot->legend->setVisible(true);
		painterThird();
		break;
	case 3:
		customPlot->legend->setVisible(true);
		painterFirst();
		break;
	case 4:
		customPlot->legend->setVisible(true);
		ui.comboBox_2->setEnabled(true);
		ui.comboBox_3->setEnabled(true);
		painterRadar();
		break;
	default:
		customPlot->legend->setVisible(true);
		break;
	}
}
void page5::pre_page() {
	setPage(parent, parent->pageFour);
}
void page5::loadData() {
	teamlabel->clear();
	ui.comboBox_2->clear();
	ui.comboBox_3->clear();
	QVector<team> teams = this->myData->getTeams();
	for (int i = 0; i < this->myData->getTeamNum(); i++) {
		this->teamlabel->append(teams[i].getName());
	}
	ui.comboBox_2->addItems(*this->teamlabel);
	ui.comboBox_3->addItems(*this->teamlabel);
}
void page5::savePdf() {

	QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss_zzz");
	QString name  = timestamp + "Result";
	customPlot->savePdf("result\\" + name + ".pdf", customPlot->width(), customPlot->height(), QCP::epAllowCosmetic);

}